# !/usr/bin/env python 
# -*- coding:utf-8 -*-
# @Author: happy 
# @Date: 2018-09-06 11:22:16 
# @Last Modified by:   happy 
# @Last Modified time: 2018-09-06 11:22:16 
# @Desc: 

# 提取目录下所有图片,更改尺寸后保存到另一目录
from PIL import Image
import os.path
import os
import glob


def convertjpg(jpgfile, outdir, width=358, height=441):
    """
    输出目录路径一定要先存在
    统一大小
    """
    # 打开图片
    img = Image.open(jpgfile)
    try:
        new_img = img.resize((width, height), Image.BILINEAR)
        new_img.save(os.path.join(outdir, os.path.basename(jpgfile)))
    except Exception as e:
        print(e)


def rename_file(filename, outdir):
    """
    文件重命名
    """
    i = 0
    path = "./"
    filelist = os.listdir(path)  # 当前文件夹下所有的文件（包含文件夹）
    for files in filelist:  # 便利所有的文件
        i += 1
        Olddir = os.path.join(path, files)  # 原来的文件路径
        if os.path.isdir(Olddir):  # 如果是文件夹 跳过
            continue
        filename = os.path.splitext(files)[0]  # 文件名
        filetype = os.path.splitext(files)[1]  # 文件拓展名
        Newdir = os.path.join(path, str(i) + filetype)  # 新文件路径
        os.rename(Olddir, Newdir)  # 重命名


yuan_pic_path = "C:\\Users\\happy\\Desktop\\听课证\\*.jpg"
gaizhihou_pic_path = "C:\\Users\\happy\\Desktop\\听课证33"
for jpgfile in glob.glob(yuan_pic_path):
    print(jpgfile)
    convertjpg(jpgfile, gaizhihou_pic_path)

print("Done")
